import { lineShaderMaterial } from '@src/core/shader'


export const mapYearToLandscapeZ = (function () {
  const yearIntervals = [
    {start: 2100, end: -300, yearLength: 1},
    // {start: 2000, end: -10, yearLength: 0.3},
  ];
  return function (year) {
    let z = 0;

    year = year || 0;

    for (let i = 0; i < yearIntervals.length; i++) {
      const iv = yearIntervals[i];

      if (year > iv.start) {
        break;
      }

      // 2000 ->
      const d = Math.min(iv.start - iv.end, iv.start - year);

      z += d * iv.yearLength;
    }

    return (1900*0.2 + 100*0.3 + 50*0.4 + 50*0.8 - z) * 12 - 4000;
  }
})();

export function initObjects () {
  var blobs = new THREE.Object3D();
  blobs.frustumCulled = false;

  var sticks = new THREE.Object3D();
  sticks.frustumCulled = false;

  return {
    blobs: blobs,
    sticks: sticks
  };
}

/*export function blobTextures () {
  var loadingPercentageMax = 0;
  var blobTextures = [];

  for (var i = 0; i < 16; i++) {
    var path = '/static/images/blob_'+ i +'.png';
    var tex = new THREE.ImageUtils.loadTexture(path, undefined, function() {
      loadingPercentageMax += 2;
    }, function() {
      loadingPercentageMax += 2;
    });

    tex.premultiplyAlpha = true;
    blobTextures.push(tex);
  }

  return blobTextures;
}*/

export function framer () {
  return (
    (window.performance && window.performance.now && window.performance.now.bind(window.performance)) ||
    Date.now.bind(Date)
  )();
}

export function getWindowSize () {
  const width = window.innerWidth
  const height = window.innerHeight

  return {
    width,
    height,
    halfX:  width / 2,
    halfY:  height / 2,
  }
}

export function makeLine (geo, mat) {
  var smat = lineShaderMaterial.clone();
  smat.uniforms =  {
    color: {type: 'v3', value: new THREE.Vector3(mat.color.r, mat.color.g, mat.color.b)}
  };
  return new THREE.Line(geo, smat);
}
